package com.hptpd.questionnairesurvey.controller.admin;

import com.google.common.collect.Maps;
import com.hptpd.questionnairesurvey.component.Result;
import com.hptpd.questionnairesurvey.service.ISurveyService;
import com.hptpd.questionnairesurvey.vo.AddSurveyVo;
import com.hptpd.questionnairesurvey.vo.SurveyVo;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* Date: 2019/10/23
 * \*
 * \* Description:
 * \
 *
 * @author walter_long
 */
@RestController
@RequestMapping("/api/admin")
public class SurveyController {
    @Resource(name = "iSurveyService")
    private ISurveyService iSurveyService;

    /**
     * 分页列出问卷
     * @param name {String}
     * @param tag {String}
     * @param page {Integer}
     * @param limit {Integer}
     * @return {Result}
     */
    @RequestMapping(value = "/survey/list", method = RequestMethod.GET)
    public Result listSurveysByPage(
            @RequestParam(defaultValue = "") String name,
            @RequestParam(defaultValue = "") String tag,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        Pageable pageable = PageRequest.of(page - 1, limit);
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("name", name);
        paramMap.put("tag", tag);
        return iSurveyService.listSurveysByPage(paramMap, pageable);
    }

    /**
     * 删除问卷
     * @param surveyVo {SurveyVo}
     * @return {Result}
     */
    @RequestMapping(value = "/survey/restart", method = RequestMethod.POST)
    public Result restartSurvey(
            @RequestBody SurveyVo surveyVo){
        return iSurveyService.restartSurvey(surveyVo);
    }

    /**
     * 发布问卷
     * @param surveyVo {SurveyVo}
     * @return {Result}
     */
    @RequestMapping(value = "/survey/stop", method = RequestMethod.POST)
    public Result stopSurvey(
            @RequestBody SurveyVo surveyVo){
        return iSurveyService.stopSurvey(surveyVo);
    }

    /**
     * 停止调查
     * @param addSurveyVo AddSurveyVo
     * @return Result
     */
    @RequestMapping(value = "/survey/add", method = RequestMethod.POST)
    public Result addSurvey(
            @RequestBody AddSurveyVo addSurveyVo){
        return iSurveyService.addSurvey(addSurveyVo);
    }

    /**
     * 调查导出
     * @param surveyId String
     * @return ResponseEntity<byte[]>
     */
    @RequestMapping(value = "/survey/{surveyId}/excel/download", method = RequestMethod.GET)
    public ResponseEntity<byte[]> exportExcel(
            @PathVariable String surveyId){
        return iSurveyService.exportExcel(surveyId);
    }
}
